package com.seeyon.apps.kk.listener.form;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.kk.listener.BaseListener;
import com.seeyon.apps.kk.manager.FreemarkFlowManager;
import com.seeyon.apps.kk.manager.LockCaigouManager;
import com.seeyon.apps.kk.manager.LockEaPricesManager;
import com.seeyon.apps.kk.manager.LockEaPricesMoreManager;
import com.seeyon.apps.kk.manager.LockKehuEduManager;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.cap4.form.modules.event.FormDataAfterSubmitEvent;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.util.annotation.ListenEvent;
import com.seeyon.kk.utils.ValueUtils;

public class EaPushLogListener extends BaseListener {
	private FreemarkFlowManager freemarkFlowManager;
	private LockEaPricesManager lepm;
	private LockEaPricesMoreManager lepmm;
	private String tablebame;
	private LockCaigouManager caigou;
	private LockKehuEduManager edu;

	public LockKehuEduManager getEdu() {
		return edu;
	}

	public void setEdu(LockKehuEduManager edu) {
		this.edu = edu;
	}

	public LockCaigouManager getCaigou() {
		return caigou;
	}

	public void setCaigou(LockCaigouManager caigou) {
		this.caigou = caigou;
	}

	public FreemarkFlowManager getFreemarkFlowManager() {
		return freemarkFlowManager;
	}

	public void setFreemarkFlowManager(FreemarkFlowManager freemarkFlowManager) {
		this.freemarkFlowManager = freemarkFlowManager;
	}

	public String getTablebame() {
		return tablebame;
	}

	public void setTablebame(String tablebame) {
		this.tablebame = tablebame;
	}

	public LockEaPricesManager getLepm() {
		return lepm;
	}

	public void setLepm(LockEaPricesManager lepm) {
		this.lepm = lepm;
	}

	public LockEaPricesMoreManager getLepmm() {
		return lepmm;
	}

	public void setLepmm(LockEaPricesMoreManager lepmm) {
		this.lepmm = lepmm;
	}

	
	@ListenEvent(event = FormDataAfterSubmitEvent.class, async = true)
	public void onFormDataAfterSubmit(FormDataAfterSubmitEvent event) throws Exception {
		Thread.sleep(BaseListener.LISTENER_THREAD_SLEEPTIME);
		FormDataMasterBean formDataMasterBean = (FormDataMasterBean) event.getSource();
		FormTableBean formTableBean = formDataMasterBean.getFormTable();
		if (formTableBean.getTableName().equals(this.tablebame)) {
			Map<String, Object> formdata1 = formDataMasterBean.getAllDataMap();
			Long again = MapUtils.getLong(formdata1, "field0014");// 重新抛送
			String ifshi = MapUtils.getString(formdata1, "field0001");// 单据类型
			String yuansj = MapUtils.getString(formdata1, "field0005");// 抛送源数据
			String UUID = MapUtils.getString(formdata1, "field0016");// UUID
			JSONObject json = JSONObject.parseObject(yuansj);
			Map<String,Object> caie=json;
			FreemarkFlowManager freemarkFlow=(FreemarkFlowManager)AppContext.getBean("kkFlowManager");

			try {
			List<Map<String,Object>> rouse = new ArrayList<Map<String,Object>>();
			rouse.add(json);
			if (ValueUtils.getConfig().getLong("kk.CXPS").equals(again)) {
				if (ifshi.equals("价格起案(一次性活动)")) {
					List<Map<String, Object>> sendToEa = lepm.sendToEa(rouse);
					List<Map<String, Object>> mv = new ArrayList<Map<String, Object>>(sendToEa);
					for (Map<String, Object> map : mv) {
						map.put("MESSAGER", MapUtils.getString(map, "XIAOXI"));
						map.put("SUI", UUID);
						String findsql = freemarkFlow.fltTemplateIntoString("sql/sap/ealog/gengxin.ftl", map);
						this.jdbcTemplate.update(findsql);
					}
					
				}
				if (ifshi.equals("价格起案(长期活动)")) {
					List<Map<String, Object>> sendToEa = lepm.sendToEa(rouse);
					List<Map<String, Object>> mv = new ArrayList<Map<String, Object>>(sendToEa);
					for (Map<String, Object> map : mv) {
						map.put("MESSAGER", MapUtils.getString(map, "XIAOXI"));
						map.put("SUI", UUID);
					}
					String findsql = freemarkFlow.fltTemplateIntoString("sql/sap/ealog/gengxin.ftl", mv);
					this.jdbcTemplate.update(findsql);
				}
				if(ifshi.equals("采购申请单")) {
					Map<String, Object> sendToSap = caigou.sendToSap(caie);
					Map<String,Object> mv = new HashMap<>();
					mv.put("STATUS", MapUtils.getString(sendToSap, "STATUS"));
					mv.put("MESSAGER", MapUtils.getString(sendToSap, "XIAOXI"));
					mv.put("SUI", UUID);
					String findsql = freemarkFlow.fltTemplateIntoString("sql/sap/ealog/gengxin.ftl", mv);
					this.jdbcTemplate.update(findsql);
				}
				if(ifshi.equals("客户额度申请")) {
					Map<String, Object> sendToSap = edu.sendToSap(caie);
					Map<String,Object> mv = new HashMap<>();
					mv.put("STATUS", MapUtils.getString(sendToSap, "STATUS"));
					mv.put("MESSAGER", MapUtils.getString(sendToSap, "XIAOXI"));
					mv.put("SUI", UUID);
					String findsql = freemarkFlow.fltTemplateIntoString("sql/sap/ealog/gengxin.ftl", mv);
					this.jdbcTemplate.update(findsql);
				}
			
			}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
			
	}
}